<?php
namespace app\controller\api;

use app\BaseController;
use app\model\SmsLogModel;
use app\model\UserModel;
use app\validate\UserValidate;

use Firebase\JWT\ExpiredException;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Firebase\JWT\SignatureInvalidException;

require root_path() . 'extend/aliyun-sms/api_demo/SmsDemo.php';

class Login extends BaseController
{
    // 登录
    public function login()
    {
        $data = $this->request->param();
        $validate = new UserValidate();
        $result = $validate->scene('login')->check($data);
        if ($result !== true) {
            $this->error($validate->getError());
        }
        // 判断验证码是否正确
        $code = SmsLogModel::where('mobile', '=', $data['mobile'])->order('create_time', 'desc')->value('sms_code');
        if (empty($code)) {
            $this->error('验证码错误');
        }
        if ($code != $data['sms_code']) {
            $this->error('验证码错误');
        }
        $smsLogModel = new SmsLogModel();
        $smsLogModel->deleteSmsCode($data['mobile']);
        // 判断是否已经注册
        $userModel = new UserModel();
        $user = $userModel->field('user_id')->getByMobile($data['mobile']);
        if (empty($user)) {
            $this->error('该手机号不存在');
        }
        $jwtContent = [
            // 签发人，这里采用当前站点域名
            'iss' => request()->domain(),
            // 签发时间，当前时间戳
            'iat' => time(),
            // 自定义数据
            'data' => [
                'user_id' => $user->user_id,
            ]
        ];
        $token = JWT::encode($jwtContent, $this->jwtKey, 'HS256');
        $this->success('登录成功', [
            'token' => $token
        ]);
    }

    // 注册
    public function register()
    {
        $data = $this->request->param();
        $validate = new UserValidate();
        $result = $validate->scene('register')->check($data);
        if ($result !== true) {
            $this->error($validate->getError());
        }
        // 判断验证码是否正确
        $code = SmsLogModel::where('mobile', '=', $data['mobile'])->order('create_time', 'desc')->value('sms_code');
        if (empty($code)) {
            $this->error('验证码错误');
        }
        if ($code != $data['check_code']) {
            $this->error('验证码错误');
        }
        // 判断是否已经注册
        $userModel = new UserModel();
        $user = $userModel->getByMobile($data['mobile']);
        if (!empty($user)) {
            $this->error('该手机号已经注册');
        }
        $userModel->mobile = $data['mobile'];
        if ($userModel->save()) {
            $this->success('注册成功');
        }
        $this->error('注册失败');
    }

    // 发送短信验证码
    public function sendSmsCode()
    {
        $mobile = input('mobile/s');
        if (empty($mobile)) {
            $this->error('请输入手机号');
        }
        // 生成随机验证码
        $code = mt_rand(100000, 999999);
        // 调用短信接口发送
        // $sendResult = \SmsDemo::sendSms($mobile,'阿洋的厚道集团','SMS_461355261',$code);
        // if ($sendResult->Code != 'OK'){
        //    $this->error($sendResult->Message);
        // }
        // 添加一条记录
        $model = new SmsLogModel();
        $model->save([
            'mobile' => $mobile,
            'sms_code' => $code,
        ]);
        $this->success('验证码发送成功：' . $code);
    }

}
